/**
 * 
 */
;
(function($) {
	$.fn.extend({
				"my_page" : function(form) {
					var $this = this;
					// 定义分页结构体
					var pageinfo = {
						url : $(this).attr("url"),
						currentPage : $(this).attr("currentPage") * 1, // 当前页码
						pageCount : $(this).attr("pageCount") * 1
					// 总页码

					};
					

					if (pageinfo.pageCount < 2)
						return false;
					// 初始起始页数、结束页数
					var start = 0, end = 10;
					if (pageinfo.currentPage >= 10)
						start = pageinfo.currentPage - 5;

					if (pageinfo.pageCount > pageinfo.currentPage + 5)
						end = pageinfo.currentPage + 5;
					else
						end = pageinfo.pageCount;
					var html = [];
					//html.push("<ul>");
					html.push("<input type=\"hidden\" name=\"currentPage\" id=\"currentPage\" value=\"1\"/>");
					if (pageinfo.currentPage != 1)
						// 如果不是第一页则有前一页
						html.push("<li class='paginate_button previous'><a>前一页</a></li>");
					if (pageinfo.pageCount > 10 && pageinfo.currentPage > 9) {
						
						html.push("<li class='paginate_button nomal'><a >1</a></li>");
						html.push("<li class='paginate_button other'><a page='"+start+"'>...</a></li>");
					}
					
					var count = 0;
					
					for (var i = start; i < end; i++) {
						if ((i + 1) == pageinfo.currentPage)
							html.push("<li class='paginate_button active'><a >" + (i + 1)
									+ "</a></li>");
						else {
							if ((i+1) < pageinfo.pageCount) {
								html.push("<li class='paginate_button nomal'><a >" + (i + 1)
										+ "</a></li>");
							} else {
								count++;
							}
						}
							
					}
					
					if((pageinfo.pageCount - pageinfo.currentPage) > 6){
						html.push("<li class='paginate_button other'><a page='"+(end+1)+"'>...</a></li>");
					}
					
					if (pageinfo.currentPage != pageinfo.pageCount){
						
						html.push("<li class='paginate_button nomal'><a >" + pageinfo.pageCount + "</a></li>");
						html.push("<li class='paginate_button next'><a >后一页</a></li>");
					}

					$this.html(html.join(""));
					// 绑定数据处理函数
					$this.find(".nomal a").bind("click", function() {
						redirectTo($(this).html());
					});
					$this.find(".previous a").bind("click", function() {
						redirectTo(pageinfo.currentPage - 1);
					});
					$this.find(".next a").bind("click", function() {
						redirectTo(pageinfo.currentPage + 1);
					});
					
					$this.find(".other a").bind("click", function() {
						redirectTo($(this).attr("page"));
					});

					function redirectTo(page) {
						var url = pageinfo.url;
						if (url.indexOf("?") == -1)
							url += "?";
						else
							url += "&";
						url += "currentPage=" + page;
						$("#currentPage").val(page);
						//debugger
						var formSerialize = $("#"+form).serializeObject();
						var v = JSON.stringify(formSerialize);
						$("#v").val(encodeURIComponent(v));
						$("form[name='" + form + "']").attr("action", url);
						$("form[name='" + form + "']").submit();

					}
					return $this;
				}
			});

})(jQuery);